﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace AndroidEditor.SettingClass
{
    class ExtensionEditor : TextBox
    {
        private bool Changed = false;
        private string doc = "";
        private ToolStripMenuItem Save = new ToolStripMenuItem("保存");
        public ExtensionEditor()
        {
            Multiline = true;
            Location = new Point(0, 0);

            ContextMenuStrip menu = new ContextMenuStrip();
            ToolStripMenuItem CloseFile = new ToolStripMenuItem("关闭");
            CloseFile.Click += new EventHandler(CloseFile_Click);
            Save.Click += new EventHandler(Save_Click);
            menu.Items.Add(Save);
            menu.Items.Add((CloseFile));
            menu.Opened += new EventHandler(menu_Opened);
            ContextMenuStrip = menu;
            TextChanged += new EventHandler(ExtensionEditor_TextChanged);
            ScrollBars = ScrollBars.Both;
        }

        void CloseFile_Click(object sender, EventArgs e)
        {
            TabPage item = (TabPage)Tag;


            if (Changed && MessageBox.Show("文件已更改，是否保存？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(doc);
                writer.Write(Text);
                writer.Flush();
                writer.Close();
            
                item.Text = Path.GetFileName(doc);
                Changed = false;
                item.Dispose();
            }
            else
            {

                item.Dispose();
            }

            
        }

        void menu_Opened(object sender, EventArgs e)
        {
            if (!Changed)
            {

                Save.Enabled = false;
            }
            else
            {
                Save.Enabled = true;
            }
        }

        void ExtensionEditor_TextChanged(object sender, EventArgs e)
        {
            if (!Changed)
            {

                TabPage item = (TabPage)Tag;
                item.Text = Path.GetFileName(doc) + "  *";
                Changed = true;
            }
        }

        void Save_Click(object sender, EventArgs e)
        {
            if (Changed)
            {
                StreamWriter writer = new StreamWriter(doc);
                writer.Close();
                TabPage item = (TabPage)Tag;
                item.Text = Path.GetFileName(doc);
                Changed = false;
            }
        }



        public void OpenFile(string doc)
        {
            this.doc = doc;
            StreamReader reader = new StreamReader(doc, System.Text.Encoding.Default);

            Text = reader.ReadToEnd();
            Text = Text.Replace("\n", "\r\n");
            TabPage item = (TabPage)Tag;
            item.Text = Path.GetFileName(doc);
            Changed = false;
            reader.Close();

            Select(0, 0);
        }
    }
}
